package com.lpwa.web.controller.system;

import java.util.List;

import com.lpwa.common.utils.StringUtils;
import com.lpwa.framework.util.ShiroUtils;
import com.lpwa.system.domain.Asset;
import com.lpwa.system.service.IAssetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lpwa.common.annotation.Log;
import com.lpwa.common.enums.BusinessType;
import com.lpwa.common.core.controller.BaseController;
import com.lpwa.common.core.page.TableDataInfo;
import com.lpwa.common.core.domain.AjaxResult;
import com.lpwa.common.utils.poi.ExcelUtil;

/**
 * 资产 信息操作处理
 * 
 * @author tanmu
 * @date 2019-12-17
 */
@Controller
@RequestMapping("/system/asset")
public class AssetController extends BaseController
{
    private String prefix = "system/asset";
	
	@Autowired
	private IAssetService assetService;
	
	@RequiresPermissions("system:asset:view")
	@GetMapping()
	public String asset()
	{
	    return prefix + "/asset";
	}
	
	/**
	 * 查询资产列表
	 */
	@RequiresPermissions("system:asset:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Asset asset)
	{
		startPage();
        List<Asset> list = assetService.selectAssetList(asset);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出资产列表
	 */
	@RequiresPermissions("system:asset:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Asset asset)
    {
    	List<Asset> list = assetService.selectAssetList(asset);
        ExcelUtil<Asset> util = new ExcelUtil<Asset>(Asset.class);
        return util.exportExcel(list, "asset");
    }
	
	/**
	 * 新增资产
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存资产
	 */
	@RequiresPermissions("system:asset:add")
	@Log(title = "资产", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Asset asset)
	{
		String tagId = asset.getTagId();
		if (!StringUtils.isEmpty(tagId)){
			asset.setTagId(tagId.toLowerCase());
		}
		asset.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(assetService.insertAsset(asset));
	}

	/**
	 * 修改资产
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		Asset asset = assetService.selectAssetById(id);
		mmap.put("asset", asset);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存资产
	 */
	@RequiresPermissions("system:asset:edit")
	@Log(title = "资产", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Asset asset)
	{		
		return toAjax(assetService.updateAsset(asset));
	}
	
	/**
	 * 删除资产
	 */
	@RequiresPermissions("system:asset:remove")
	@Log(title = "资产", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(assetService.deleteAssetByIds(ids));
	}
	
}
